<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!--      <el-form-item label="联系人id" prop="contactsId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.contactsId"-->
      <!--          placeholder="请输入联系人id"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="联系人" prop="contactsName">
        <el-input
          v-model="queryParams.contactsName"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="contactsPhone">
        <el-input
          v-model="queryParams.contactsPhone"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="联系邮箱" prop="contactsEmail">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.contactsEmail"-->
      <!--          placeholder="请输入联系邮箱"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="备用" prop="note">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.note"-->
      <!--          placeholder="请输入备用"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="是否新消息" prop="isNew">
        <!--        <el-input-->
        <!--          v-model="queryParams.isNew"-->
        <!--          placeholder="是否新消息"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <el-select v-model="queryParams.isNew" placeholder="是否新消息" clearable>
          <el-option
            v-for="dict in isNews"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-select v-model="queryParams.sort" clearable placeholder="请选择">
          <el-option
            v-for="dict in sortType"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="是否新 用户" prop="isNewo">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.isNewo"-->
      <!--          placeholder="请输入是否新 用户"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['information:cooperateBack:add']"-->
      <!--        >新增</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['information:cooperateBack:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['information:cooperateBack:remove']"
        >删除
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['information:cooperateBack:export']"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cooperateBackList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <!--      <el-table-column label="联系人id" align="center" prop="contactsId" />-->
      <el-table-column label="联系人" align="center" prop="contactsName"/>
      <el-table-column label="公司地址" align="center" prop="contactsAddress"/>
      <el-table-column label="联系方式" align="center" prop="contactsPhone"/>
      <el-table-column label="联系邮箱" align="center" prop="contactsEmail"/>
      <!--      <el-table-column label="内容" align="center" prop="content" />-->
      <!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <!--      <el-table-column label="备用" align="center" prop="note" />-->
      <el-table-column label="是否新消息" align="center" prop="isNew">
        <template slot-scope="scope">
          <span v-if="scope.row.isNew==1">是</span>
          <span v-if="scope.row.isNew==0">否</span>
        </template><!---->
      </el-table-column>
      <!--      <el-table-column label="是否新 用户" align="center" prop="isNewo" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['information:cooperateBack:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['information:cooperateBack:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商务合作对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="759px" :close-on-click-modal='false' append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <!--        <el-form-item label="联系人id" prop="contactsId">-->
        <!--          <el-input v-model="form.contactsId" placeholder="请输入联系人id" />-->
        <!--        </el-form-item>-->
        <el-form-item label="联系人" prop="contactsName">
          <el-input class="kind" v-model="form.contactsName" disabled placeholder="请输入联系人"/>
        </el-form-item>
        <el-form-item label="公司地址" prop="contactsAddress">
          <el-input class="kind" v-model="form.contactsAddress" disabled type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="联系方式" prop="contactsPhone">
          <el-input class="kind" v-model="form.contactsPhone" disabled placeholder="请输入联系方式"/>
        </el-form-item>
        <el-form-item label="联系邮箱" prop="contactsEmail">
          <el-input class="kind" v-model="form.contactsEmail" disabled placeholder="请输入联系邮箱"/>
        </el-form-item>
        <!--        <el-form-item label="内容">-->
        <!--          <editor v-model="form.content" :min-height="192"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="备注" prop="remark">-->
        <!--          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="备用" prop="note">-->
        <!--          <el-input v-model="form.note" placeholder="请输入备用" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="是否新 管理员" prop="isNew">-->
        <!--          <el-input v-model="form.isNew" placeholder="请输入是否新" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="是否新 用户" prop="isNewo">-->
        <!--          <el-input v-model="form.isNewo" placeholder="请输入是否新 用户" />-->
        <!--        </el-form-item>-->
        <el-divider content-position="center">商务合作聊天记录</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddCooperateContent">添加
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteCooperateContent">删除
            </el-button>
          </el-col>
        </el-row>
        <el-table :data="cooperateContentList" :row-class-name="rowCooperateContentIndex"
                  @selection-change="handleCooperateContentSelectionChange" ref="cooperateContent">
          <el-table-column type="selection" width="50" align="center"/>
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="内容" prop="content" align="center" width="180">
            <template slot-scope="scope">
              <el-input v-model="scope.row.content" type="textarea" placeholder="请输入内容"/>
            </template>
          </el-table-column>
          <el-table-column align="center" label="创建时间" prop="createTime" width="280">
            <template slot-scope="scope">
              <!--              <el-date-picker-->
              <!--                v-model="scope.row.createTime"-->
              <!--                value-format="yyyy-MM-dd HH:mm:ss"-->
              <!--                type="datetime"-->
              <!--                placeholder="选择日期时间">-->
              <!--              </el-date-picker>-->
              <span>{{ scope.row.createTime }}</span>
            </template>
          </el-table-column>
          <el-table-column label="是否管理员发送" prop="isAdmin" align="center" width="150">
            <template slot-scope="scope">
              <span v-if="scope.row.isAdmin==1">是</span>
              <span v-if="scope.row.isAdmin==0">否</span>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addCooperateBack,
  delCooperateBack,
  getCooperateBack,
  listCooperateBack,
  updateCooperateBack
} from "@/api/information/cooperateBack";

export default {
  name: "CooperateBack",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedCooperateContent: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商务合作表格数据
      cooperateBackList: [],
      // 商务合作记录表格数据
      cooperateContentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isNews: [
        {
          label: "否",
          value: 0
        },
        {
          label: "是",
          value: 1
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contactsId: null,
        contactsName: null,
        contactsAddress: null,
        contactsPhone: null,
        contactsEmail: null,
        content: null,
        note: null,
        isNew: null,
        isNewo: null,
        sort: null
      },
      sortType: [
        {
          value: 0,
          label: "按时间倒序"
        },
        {
          value: 1,
          label: "按时间正序"
        },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商务合作列表 */
    getList() {
      this.loading = true;
      listCooperateBack(this.queryParams).then(response => {
        this.cooperateBackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        contactsId: null,
        contactsName: null,
        contactsAddress: null,
        contactsPhone: null,
        contactsEmail: null,
        content: null,
        remark: null,
        createTime: null,
        updateTime: null,
        note: null,
        isNew: null,
        isNewo: null
      };
      this.cooperateContentList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商务合作";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCooperateBack(id).then(response => {
        this.form = response.data;
        this.cooperateContentList = response.data.cooperateContentList;
        this.open = true;
        this.title = "修改商务合作";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.cooperateContentList = this.cooperateContentList;
          if (this.form.id != null) {
            updateCooperateBack(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCooperateBack(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商务合作编号为"' + ids + '"的数据项？').then(function () {
        return delCooperateBack(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 商务合作记录序号 */
    rowCooperateContentIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 商务合作记录添加按钮操作 */
    handleAddCooperateContent() {
      let obj = {};
      obj.isAdmin = 1;
      obj.content = "";
      obj.remark = "";
      let year = new Date().getFullYear(); //获取当前时间的年份
      let month = new Date().getMonth() + 1; //获取当前时间的月份
      let day = new Date().getDate(); //获取当前时间的天数
      let hours = new Date().getHours(); //获取当前时间的小时
      let minutes = new Date().getMinutes(); //获取当前时间的分数
      let seconds = new Date().getSeconds(); //获取当前时间的秒数
      //当小于 10 的是时候，在前面加 0
      if (hours < 10) {
        hours = "0" + hours;
      }
      if (minutes < 10) {
        minutes = "0" + minutes;
      }
      if (seconds < 10) {
        seconds = "0" + seconds;
      }
      //拼接格式化当前时间
      obj.createTime = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;

      this.cooperateContentList.push(obj);
    },
    /** 商务合作记录删除按钮操作 */
    handleDeleteCooperateContent() {
      if (this.checkedCooperateContent.length == 0) {
        this.$modal.msgError("请先选择要删除的商务合作记录数据");
      } else {
        const cooperateContentList = this.cooperateContentList;
        const checkedCooperateContent = this.checkedCooperateContent;
        this.cooperateContentList = cooperateContentList.filter(function (item) {
          return checkedCooperateContent.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleCooperateContentSelectionChange(selection) {
      this.checkedCooperateContent = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('information/cooperateBack/export', {
        ...this.queryParams
      }, `cooperateBack_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.kind {
  width: 90%;
}
</style>
